package com.jet.merchant.service.impl;

import com.jet.core.utils.IdUtil;
import com.jet.core.utils.IpUtil;
import com.jet.merchant.domain.entity.ClerkAccountLoginLog;
import com.jet.merchant.mapper.ClerkAccountLoginLogMapper;
import com.jet.merchant.service.IClerkAccountLoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-02-25
 */
@Service
public class ClerkAccountLoginLogServiceImpl extends ServiceImpl<ClerkAccountLoginLogMapper, ClerkAccountLoginLog> implements IClerkAccountLoginLogService {

    @Override
    @Async("taskExecutor")
    public void save(HttpServletRequest request, String accountId, int type, LocalDateTime loginTime, int platform) {
        String ip = IpUtil.getIpAddress(request);
        String userAgent = request.getHeader("User-Agent");

        ClerkAccountLoginLog entity = new ClerkAccountLoginLog();
        entity.setId(IdUtil.generateLower());
        entity.setAccountId(accountId);
        entity.setType(type);
        entity.setLoginTime(loginTime);
        entity.setIp(ip);
        entity.setPlatform(platform);
        entity.setUserAgent(userAgent);
        entity.setCreateTime(LocalDateTime.now());
        super.save(entity);
    }
}
